package newWork.controller;

import newWork.constant.ResultConstant;
import newWork.entity.CommonResult;
import newWork.entity.LayUITable;
import newWork.entity.StaffEntity;
import newWork.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {


	@Autowired
	private IStaffService service;
	
	/**
	 * 查询
	 *
	 * @return
	 */
	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public CommonResult select(@RequestBody StaffEntity entity) {
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, service.select(entity));
	}


	/**
	 * 查询列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public LayUITable<StaffEntity> list(Integer page, Integer limit) {
		StaffEntity staffEntity = new StaffEntity();
		Integer newPage = (page - 1) * limit;
		staffEntity.setPageSize(limit);
		staffEntity.setStart(newPage);
		int count = Integer.parseInt(service.likeSelectCount(new StaffEntity()).toString());

		List<StaffEntity> select = service.select(staffEntity);
		return new LayUITable<>(0, "SUCCESS", count, select);
	}


	/**
	 * 更新
	 *
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CommonResult update(@RequestBody StaffEntity entity) {
		service.update(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 添加
	 *
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public CommonResult add(@RequestBody StaffEntity entity) {
		service.add(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 删除
	 *
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public CommonResult delete(@RequestBody StaffEntity entity) {
		service.delete(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

}
